package com.aoxin.service.impl.client;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.client.VipRelevantMapper;
import com.aoxin.mapper.course.webCourse.SlfChildrenMapper;
import com.aoxin.model.course.SlfChildren;
import com.aoxin.model.opm.SlfUser;
import com.aoxin.service.inter.client.VipRelevantService;
import com.aoxin.service.inter.course.webCourse.ChildrenService;
import com.aoxin.service.inter.opm.UserService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.RedisUtil;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.UUID;

/**
 * VIP 相关impl
 * @author Administrator
 *
 */
@Service("VipRelevantService")
public class VipRelevantServiceImpl implements VipRelevantService {

	@Autowired
	VipRelevantMapper vipRelevantMapper;
	
	@Autowired
	private UserService userService;
	@Autowired
	private SlfChildrenMapper slfChildrenMapper;
	@Override
	public Map<String, Object> queryVipPrice() {
		
		/***
		 * VIP 用户不展示数据
		 */
		try {
			if(ThreadLocalUtil.getUser()==null){
				return this.vipRelevantMapper.queryVipPrice();
			}else{
				SlfUser slfUser = userService.getUser(ThreadLocalUtil.getUser().getUserId());
				if(slfUser.getFlag()==1){
					return new HashMap<String,Object>();
				}else{
					return this.vipRelevantMapper.queryVipPrice();
				}
			}
		} catch (Exception e) {
			return this.vipRelevantMapper.queryVipPrice();
		}
	}
	
	
	@Override
	@Transactional
	public void updateUserFlag() {
		Assert.notNull(ThreadLocalUtil.getUser(), String.format(ErrorUtil.PARAMETER_NULL, "user"));
		SlfChildren slfChildren= slfChildrenMapper.getAChildren(ThreadLocalUtil.getChildren().getChildrenId());
		//校验用户是否已购买
		DateTime vipTermOfValidity=new DateTime(slfChildren.getVipEffectiveTime());
		DateTime now=new DateTime();
		if(slfChildren.getVipLv()==1&&now.isBefore(vipTermOfValidity.plusYears(1))){
			//vip 并且是有效期内
			throw new IllegalArgumentException("已购买尊享用户标志无需再次购买");
		}
		//获取VIP 价格
		Map<String,Object> resultPriceMap=this.vipRelevantMapper.queryVipPrice();
		SlfUser slfUser = userService.getUser(ThreadLocalUtil.getUser().getUserId());
		Double vipPrice=Double.parseDouble(resultPriceMap.get("commodity_price").toString()); 
		Double userLfb=slfUser.getLfb();
		boolean flag=userLfb<vipPrice?true:false;
		if (flag) {
			throw new IllegalArgumentException(ErrorUtil.NOT_ENOUGH_BALANCE);
		}
		BigDecimal userLfbBig=new BigDecimal(userLfb.toString()); 
		BigDecimal vipPriceBig=new BigDecimal(vipPrice.toString());
		BigDecimal lastvalue=userLfbBig.subtract(vipPriceBig);
		vipRelevantMapper.updateUserFlag(slfUser.getUserId(), lastvalue);
		this.vipRelevantMapper.updateChildrenFlag(ThreadLocalUtil.getChildren().getChildrenId());
		
		RedisUtil.del(slfUser.getPhone() + slfUser.getUserId() + ChildrenService.REDIS_CHILDREN_CODE);
		RedisUtil.set(slfUser.getPhone() + slfUser.getUserId() + ChildrenService.REDIS_CHILDREN_CODE, slfChildren);
		
		
		
	}

	
	
	
}
